#include "BufferBlock.h"
//#include "ScanBuffer.h"

BufferBlock::BufferBlock(int size, char* block, int eof_pos) {
	this->block = block;
	this->size = size;
	this->eof_pos = eof_pos;
	
	this->nextBlock = 0;
	this->prevBlock = 0;
	this->pos = 0;
	
}

BufferBlock::~BufferBlock() {
	delete [] this->block;
}


/**
* Gibt den Block-Status zurück.
* -1 = interner Zeiger steht am Anfang
* 0 = interner Zeiger ist irgendwo, aber nicht am Anfang oder Ende
* 1 = interner Zeiger ist am Ende angekommen
*/
int BufferBlock::getStatus() {
	if(pos == eof_pos)
		return 2;
	
	if(pos == 0)
		return -1;
		
	if(pos > 0 && pos < size)
		return 0;
		
	if(pos == size)
		return 1;
	
	// should never be reached
	return 1;
}

char BufferBlock::getNextChar() {
	if(this->eof_pos > 0 && this->pos >= this->eof_pos)
		throw ScanBufferIOException((char*) "End Of File!");
	
	return this->block[this->pos++];
}

BufferBlock* BufferBlock::rewindChars(int count) {
	int div = count - this->pos;
	
	if(div > 0 && this->prevBlock != 0) {
		this->pos = 0;
		return this->prevBlock->rewindChars(div);
		
	} else {
		this->pos = pos - count;
	}
	
	return this;
}


BufferBlock* BufferBlock::getNextBlock() {
	return this->nextBlock;
}

BufferBlock* BufferBlock::getPrevBlock() {
	return this->prevBlock;
}

void BufferBlock::setNextBlock(BufferBlock* block) {
	this->nextBlock = block;
}

void BufferBlock::setPrevBlock(BufferBlock* block) {
	this->prevBlock = block;
}
